#include "widget.h"
#include "form.h"
#include <QMessageBox>
#include <QEvent>
#include <QKeyEvent>
#include "ui_widget.h"
#include <QDebug>


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //setFixedSize(300,200);
    ui->Yonghuming->setMaxLength(6);
    ui->Mima->setMaxLength(6);
    ui->Mima->installEventFilter(this);
    form = new Form;

    QPixmap pixmap;
    pixmap.load(":/image/Guanliyuan.png");
    if (!pixmap.isNull())
    {
        QSize labelSize = ui->ImageDl->size();
        pixmap = pixmap.scaled(labelSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);// Qt::SmoothTransformation用于提高缩放时的图像质量
        ui->ImageDl->setPixmap(pixmap);
    }
    //加入样式到下拉框
    QStringList files;
    files<<"AMOLED.qss"<<"Aqua.qss"<<"ConsoleStyle.qss"<<"ElegantDark.qss"<<"MacOS.qss"<<"ManjaroMix.qss"
          <<"MaterialDark.qss"<<"MetroUI.qss"<<"NeonButtons.qss"<<"Ubuntu.qss";



    this->ui->com_style->addItems(files);

}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_Denglu_clicked()
{
//    QString InputUserName = ui->Yonghuming->text();
//    QString InputPassWord = ui->Mima->text();

//    if(InputUserName == "Admin" && InputPassWord == "1234")
//    {
        this->hide();
        //    }
        //切换样式
        QString path= QString(":/qss/%1").arg(this->ui->com_style->currentText());
        QFile file(path);
        if (!file.open(QIODevice::ReadOnly))
            exit(0);

        QTextStream in(&file);
        QString css = in.readAll();
        //应用程序对象的QApplication::setStyleSheet
        qApp->setStyleSheet(css);
        this->form->show();
//    }
//    else if(InputUserName == "" || InputPassWord == "")
//    {
//        QMessageBox::information(this,"注意","请输入账号或密码!");
//        //QMessageBox::information(nullptr, QString::fromLocal8Bit(" "), QString::fromLocal8Bit("请输入用户名或密码"));
//    }
//    else
//    {
//        //QMessageBox::critical(nullptr, QString::fromLocal8Bit("警告"), QString::fromLocal8Bit("用户名或密码错误！"));
//        QMessageBox::critical(this,"警告","用户名或密码错误！");
//    }
}

bool Widget::eventFilter(QObject *obj,QEvent *event)
{
    if( (obj == ui->Mima) && (event->type() == QEvent::KeyPress))
    {
        //qDebug() << "123456";
        QKeyEvent *keyEvent = dynamic_cast<QKeyEvent *>(event);
        if(keyEvent->text() == '\r')
        {
            //qDebug() << "123456";
            on_Denglu_clicked();
            return true;
        }

    }
    return QWidget::eventFilter(obj,event);
}


